﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLZJFurnitureSCM.Model;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Data;

namespace QLZJFurnitureSCM.DAL
{
    public class OrderProductDAO:IBaseDAO<OrderProduct>
    {
        private string connectionString = DALFactory.CONNECTIONSTRING;

        private SqlConnection sqlConn = null;

        public OrderProductDAO()
        {
            sqlConn = new SqlConnection(connectionString);
        }

        public OrderProduct Create(OrderProduct t)
        {
            try
            {
                string query = string.Format("INSERT INTO [OrderProduct]([productName],[dimension],[buyCount],[price],[orderNo],[orderId],[productId]) values('{0}','{1}',{2},{3},'{4}',{5},{6}); select @@identity;",
                                                        t.ProductName, t.Dimension, t.BuyCount, t.Price, t.OrderNo, t.OrderId, t.ProductId);
                if (sqlConn.State != ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand sqlCmd = new SqlCommand(query, sqlConn);
                Console.WriteLine(query);
                object o = sqlCmd.ExecuteScalar();
                sqlConn.Close();
                if (o != null)
                    t.OrderProductId = int.Parse(o.ToString());
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
                return Create(t);
            }
            return t;
        }


        public OrderProduct Read(OrderProduct t)
        {
            throw new NotImplementedException();
        }

        public bool Update(OrderProduct t)
        {
            throw new NotImplementedException();
        }

        public bool Delete(OrderProduct t)
        {
            throw new NotImplementedException();
        }

        public List<OrderProduct> ReadAll()
        {
            DataContext dataContext = new DataContext(connectionString);
            IEnumerable<OrderProduct> enumOrderProduct = dataContext.ExecuteQuery<OrderProduct>("select * from OrderProduct;");
            return enumOrderProduct.ToList();
        }
    }
}
